﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace Pongster
{
    public class LeftRightSelector : Control
    {
        public event EventHandler SelectionChanged;

        List<string> items = new List<string>();

        Texture2D leftTexture;
        Texture2D rightTexture;
        Texture2D stopTexture;

        Color leftColor = Color.White;
        Color rightColor = Color.White;

        bool needsEnter = false;

        Color selectedColor = Color.LimeGreen;
        int maxItemWidth;
        int selectedItem;

        Rectangle areaRect;
        Rectangle leftRect;
        Rectangle rightRect;

        public Rectangle AreaRect
        {
            get { return areaRect; }
            set { areaRect = value; }
        }

        public bool NeedsEnter
        {
            get { return needsEnter; }
            set { needsEnter = value; }
        }

        public Color SelectedColor
        {
            get { return selectedColor; }
            set { selectedColor = value; }
        }

        public int SelectedIndex
        {
            get { return selectedItem; }
            set { selectedItem = (int)MathHelper.Clamp(value, 0f, items.Count); }           
        }

        public string SelectedItem
        {
            get { return items[selectedItem]; }
        }

        public List<string> Items
        {
            get { return items; }
        }

        public LeftRightSelector(Texture2D leftArrow, Texture2D rightArrow, Texture2D stopArrow, bool needBool)
        {
            leftTexture = leftArrow;
            rightTexture = rightArrow;
            stopTexture = stopArrow;
            TabStop = true;
            Color = Color.White;
            needsEnter = needBool;
        }

        public void SetItems(string[] items, int maxWidth)
        {
            this.items.Clear();
            foreach (string s in items)
                this.items.Add(s);

            maxItemWidth = maxWidth;
        }

        protected void OnSelectionChanged()
        {
            if (SelectionChanged != null)
            {
                SelectionChanged(this, null);
            }
        }


        public override void Update(GameTime gameTIme)
        {
            areaRect = new Rectangle((int)Position.X, (int)Position.Y, (maxItemWidth) / 3 + maxItemWidth, (int)SpriteFont.MeasureString(items[selectedItem]).Y);
            leftRect = new Rectangle((int)position.X, (int)position.Y, leftTexture.Width, leftTexture.Height);
            rightRect = new Rectangle((int)position.X + (maxItemWidth) / 6 + maxItemWidth, (int)position.Y, rightTexture.Width, rightTexture.Height);

            if (leftRect.Contains(InputHandler.MousePoint))
                leftColor = Color.White;
            else
                leftColor = Color.CadetBlue;

            if (rightRect.Contains(InputHandler.MousePoint))
                rightColor = Color.White;
            else
                rightColor = Color.CadetBlue;

            if (leftRect.Contains(InputHandler.MousePoint) && InputHandler.LeftMouseClick())
            {
                selectedItem--;
                if (selectedItem < 0)
                    selectedItem = 0;
                OnSelectionChanged();
            }

            if (rightRect.Contains(InputHandler.MousePoint) && InputHandler.LeftMouseClick())
            {
                selectedItem++;
                if (selectedItem >= items.Count)
                    selectedItem = items.Count - 1;
                OnSelectionChanged();
            }
            
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Vector2 drawTo = position;

            if (selectedItem != 0)
                spriteBatch.Draw(leftTexture, drawTo, leftColor);
            else
                spriteBatch.Draw(stopTexture, drawTo, Color.CadetBlue);

            drawTo.X += leftTexture.Width + 5f;

            float itemWidth = spriteFont.MeasureString(items[selectedItem]).X;
            float offset = (maxItemWidth - itemWidth) / 2;

            drawTo.X += offset;

            if (hasFocus)
               // spriteBatch.DrawString(spriteFont, items[selectedItem], drawTo, selectedColor);
                spriteBatch.DrawString(SpriteFont, items[selectedItem], drawTo, selectedColor, Rotation, Origin, ScaleV, SpriteEffects.None, 1f);
            else
               // spriteBatch.DrawString(spriteFont, items[selectedItem], drawTo, Color);
                spriteBatch.DrawString(SpriteFont, items[selectedItem], drawTo, Color, Rotation, Origin, ScaleV, SpriteEffects.None, 1f);


            drawTo.X += -1 * offset + maxItemWidth + 5f;

            if (selectedItem != items.Count - 1)
                spriteBatch.Draw(rightTexture, drawTo, rightColor);
            else
                spriteBatch.Draw(stopTexture, drawTo, Color.CadetBlue);

        }

        public override void HandleInput()
        {
            if (items.Count == 0)
                return;

            if (needsEnter == false)
            {
                if (InputHandler.KeyReleased(Keys.Left) || InputHandler.KeyReleased(Keys.A))
                {
                    selectedItem--;
                    if (selectedItem < 0)
                        selectedItem = 0;
                    OnSelectionChanged();
                }

                if (InputHandler.KeyReleased(Keys.Right) || InputHandler.KeyReleased(Keys.D))
                {
                    selectedItem++;
                    if (selectedItem >= items.Count)
                        selectedItem = items.Count - 1;
                    OnSelectionChanged();
                }
            }
            else if (needsEnter == true)
            {
                if (InputHandler.KeyReleased(Keys.Left) || InputHandler.KeyReleased(Keys.A))
                {
                    selectedItem--;
                    if (selectedItem < 0)
                        selectedItem = 0;
                }

                if (InputHandler.KeyReleased(Keys.Right) || InputHandler.KeyReleased(Keys.D))
                {
                    selectedItem++;
                    if (selectedItem >= items.Count)
                        selectedItem = items.Count - 1;
                }

                if (InputHandler.KeyReleased(Keys.Enter) || InputHandler.KeyReleased(Keys.Space))
                    OnSelectionChanged();
            }


        }


    }
}
